<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="close()"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="病人姓名" prop="patientId">
        <!-- <el-input v-model="dataForm.patientId" placeholder=""></el-input> -->
        <el-select
          v-model="patientValue"
          filterable
          placeholder="请选择"
          style="width: 100%"
          @change="setPatientName"
        >
          <el-option
            v-for="item in patientOption"
            :key="item.id"
            :label="item.name"
            :value="{ value: item.id, label: item.name }"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="病人姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item> -->
      <el-form-item label="出院时间" prop="outTime">
        <!-- <el-input v-model="dataForm.outTime" placeholder=""></el-input> -->
        <el-date-picker
          v-model="dataForm.outTime"
          type="date"
          placeholder="出院时间"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="负责人" prop="doctorId">
        <!-- <el-input v-model="dataForm.doctorId" placeholder=""></el-input> -->
        <el-select
          v-model="dataForm.doctorId"
          filterable
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in doctorOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      patientValue: null,
      patientOption: null,
      doctorOption: null,
      visible: false,
      dataForm: {
        id: 0,
        patientId: "",
        name: "",
        outTime: "",
        doctorId: "",
      },
      dataRule: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        outTime: [{ required: true, message: "不能为空", trigger: "blur" }],
        doctorId: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getPatientOptions();
     this.getdoctorOptions();
  },
  methods: {
    getPatientOptions() {
      this.$http({
        url: this.$http.adornUrl("/sys/patient/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.patientOption = data.page.list;
        }
      });
    },
    setPatientName(val) {
      const { value, label } = val;
      this.dataForm.name = label;
      this.dataForm.patientId = value;
    },
    getdoctorOptions() {
      this.$http({
        url: this.$http.adornUrl("/sys/doctor/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.doctorOption = data.page.list;
        }
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/checkout/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.patientId = data.checkOut.patientId;
              this.dataForm.name = data.checkOut.name;
              this.dataForm.outTime = data.checkOut.outTime;
              this.dataForm.doctorId = data.checkOut.doctorId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/checkout/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              patientId: this.dataForm.patientId,
              name: this.dataForm.name,
              outTime: this.dataForm.outTime,
              doctorId: this.dataForm.doctorId,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    close() {
      this.patientValue = null;
      this.dataForm.name = null;
      this.dataForm.patientId = null;
    },
  },
};
</script>
